//
//  TTUserViewController.m
//  iNeedish
//
//  Created by Felipe Navarro V. on 28-10-09.
//  Copyright 2009 deis. All rights reserved.
//

#import "TTUserViewController.h"
#import "UsersModel.h"
#import "UsersViewDataSource.h"
#import "UsersResultsModel.h"


#import <QuartzCore/QuartzCore.h>

#import "INDefaultStylesheet.h"
#import "User.h"
#import "NeedishStore.h"

//static CGFloat kImageWidth = 57;
//static CGFloat kImageHeight = 57;
static const CGFloat kSmallMargin = 6;




@implementation TTUserViewController


@synthesize user_id;

- (id)init {
	if (self = [super init]) {
//		_Helps = [[TTTableViewController alloc] init];
		user_id = nil;
	}
	return self;
}



- (void)loadView
{
	UIImage* image = [UIImage imageNamed:@"Needs.png"];
	//self.tabBarItem = [[[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemMostViewed tag:1] autorelease];
	self.tabBarItem = [[UITabBarItem alloc] initWithTitle:self.title image:image tag:1];
	
    // Create the tableview.
	self.variableHeightRows = YES;	
    self.view = [[[UIView alloc] initWithFrame:TTApplicationFrame()] autorelease];
    self.tableView = [[[UITableView alloc] initWithFrame:TTApplicationFrame() style:UITableViewStylePlain] autorelease];
    self.tableView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    [self.view addSubview:self.tableView];
	[self.tableView reloadData];
    
}	

- (id)initWithUser:(NSString*) user {
//	TTLOG(@"we initWithUser: %@ ", user);
	if (self = [super init]) {
		self.user_id = user;
		self.hidesBottomBarWhenPushed = YES;
		self.statusBarStyle = UIStatusBarStyleBlackOpaque;
		self.navigationBarTintColor = TTSTYLEVAR(needishBlueColor);
		self.tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;	
		
		self.tableViewStyle = UITableViewStyleGrouped;
//*********		
//		self.variableHeightRows = YES;
		self.title = @"Profile";
//		self.title = [NSString stringWithFormat:@"%@", username];
		
		NSString *username, *password;
		
		username = [NeedishStore getUserName];
		password = [NeedishStore getPasswordForUsername:username];
		if ((!self.dataSource) && (username && password)) { 
			
			id<TTTableViewDataSource>ds_user = [UsersViewDataSource dataSourceWithItems:nil];
			ds_user.model = CreateUsersModelWithCurrentSettings(@"users", @"profile", 0,  self.user_id);
			[ds_user load:TTURLRequestCachePolicyDefault more:YES];	
			
			self.dataSource = ds_user;
			[self.tableView reloadData];
		}

//		self.tableView.allowsSelection = FALSE;
//*****		
	}
	
	
	return self;
}



////////////////////////////////////////////////////////////////////////////////////
#pragma mark TTViewController

- (UIImage*)imageForError:(NSError*)error
{
    return [UIImage imageNamed:@"Three20.bundle/images/error.png"];
}

-(void)viewDidDisappear:(BOOL)animated {
	self.hidesBottomBarWhenPushed = NO;
}

//- (void)didReceiveMemoryWarning 
//{ 
//} 
//
- (void)dealloc	{
	TT_RELEASE_SAFELY(user_id);
    [super dealloc];
}


@end

